package com.ruoyi.cms.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.BaseSelect;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.CmsArticleCategoryMapper;
import com.ruoyi.cms.domain.CmsArticleCategory;
import com.ruoyi.cms.service.ICmsArticleCategoryService;

/**
 * 文章分类Service业务层处理
 * 
 * @author zouhuu
 * @date 2022-08-04
 */
@Service
public class CmsArticleCategoryServiceImpl implements ICmsArticleCategoryService 
{
    @Autowired
    private CmsArticleCategoryMapper cmsArticleCategoryMapper;

    /**
     * 是否存在分类子节点
     *
     * @param categoryId 分类ID
     * @return 结果
     */
    @Override
    public boolean hasChildByCategoryId(Long categoryId) {
        int result = cmsArticleCategoryMapper.hasChildByCategoryId(categoryId);
        return result > 0;
    }

    /**
     * 查询文章分类选项
     *
     * @param cmsArticleCategory 文章分类
     * @return 文章分类集合
     */
    @Override
    public List<BaseSelect> selectCmsArticleCategoryOptions(CmsArticleCategory cmsArticleCategory) {
        List<CmsArticleCategory> cmsArticleCategories = cmsArticleCategoryMapper.selectCmsArticleCategoryOptions(cmsArticleCategory);

        // 构建基本分类选项
        List<BaseSelect> baseCategories = cmsArticleCategories
                .stream()
                .map(item -> new BaseSelect(item.getId(), item.getParentId(), item.getCategoryName(), null))
                .collect(Collectors.toList());

        // 构建树结构
        List<BaseSelect> collect = baseCategories.stream().filter(m -> m.getPid() == 0).map(
                item -> {
                    item.setChildren(getChildrenList(item, baseCategories));
                    return item;
                }
        ).collect(Collectors.toList());

        return collect;
    }

    /**
     * 获取子节点列表
     * @param tree
     * @param list
     * @return
     */
    public static List<BaseSelect> getChildrenList(BaseSelect tree, List<BaseSelect> list){
        List<BaseSelect> children = list.stream().filter(item -> Objects.equals(item.getPid(), tree.getId())).map(
                (item) -> {
                    item.setChildren(getChildrenList(item, list));
                    return item;
                }
        ).collect(Collectors.toList());
        return children;
    }


    /**
     * 查询文章分类
     * 
     * @param id 文章分类主键
     * @return 文章分类
     */
    @Override
    public CmsArticleCategory selectCmsArticleCategoryById(Long id)
    {
        return cmsArticleCategoryMapper.selectCmsArticleCategoryById(id);
    }

    /**
     * 查询文章分类列表
     * 
     * @param cmsArticleCategory 文章分类
     * @return 文章分类
     */
    @Override
    public List<CmsArticleCategory> selectCmsArticleCategoryList(CmsArticleCategory cmsArticleCategory)
    {
        return cmsArticleCategoryMapper.selectCmsArticleCategoryList(cmsArticleCategory);
    }

    /**
     * 新增文章分类
     * 
     * @param cmsArticleCategory 文章分类
     * @return 结果
     */
    @Override
    public int insertCmsArticleCategory(CmsArticleCategory cmsArticleCategory)
    {
        cmsArticleCategory.setCreateTime(DateUtils.getNowDate());
        return cmsArticleCategoryMapper.insertCmsArticleCategory(cmsArticleCategory);
    }

    /**
     * 修改文章分类
     * 
     * @param cmsArticleCategory 文章分类
     * @return 结果
     */
    @Override
    public int updateCmsArticleCategory(CmsArticleCategory cmsArticleCategory)
    {
        cmsArticleCategory.setUpdateTime(DateUtils.getNowDate());
        return cmsArticleCategoryMapper.updateCmsArticleCategory(cmsArticleCategory);
    }

    /**
     * 批量删除文章分类
     * 
     * @param ids 需要删除的文章分类主键
     * @return 结果
     */
    @Override
    public int deleteCmsArticleCategoryByIds(Long[] ids)
    {
        return cmsArticleCategoryMapper.deleteCmsArticleCategoryByIds(ids);
    }

    /**
     * 删除文章分类信息
     * 
     * @param id 文章分类主键
     * @return 结果
     */
    @Override
    public int deleteCmsArticleCategoryById(Long id)
    {
        return cmsArticleCategoryMapper.deleteCmsArticleCategoryById(id);
    }
}
